﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class ThueList : System.Web.UI.Page
    {
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung();
            if (!IsPostBack)
            {
                LoadHienthi();
            }

        }
        public void LoadHienthi()
        {
            IUnitOfWork iunfwork = new EFUnitOfWork();
            THUERepository repository = new THUERepository(new EFRepository<THUE>(), iunfwork);
            IQueryable<THUE> objThue = repository.All();
            Grthue.DataSource = objThue;
            Grthue.DataBind();
        }
        public void KiemtraQUyenSuDung()
        {
            lbtnthemmoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemmoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemmoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemmoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void Grthue_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                Response.Redirect("ThueAdd.aspx?MATH=" + e.CommandArgument);
            }
            if (e.CommandName == "lbtnxoa")
            {
                IUnitOfWork iunitfwork = new EFUnitOfWork();
                THUERepository repository=new THUERepository(new EFRepository<THUE>(),iunitfwork);
                THUE objthue = repository.GetById(e.CommandArgument.ToString());
                if (objthue != null)
                {
                    repository.Delete(objthue);
                    repository.Save();
                    LoadHienthi();
                }
            }
        }

        protected void Grthue_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            lbtnsua.Visible = false;
            lbtnxoa.Visible = false;
            if (objchucnang != null)
            {
                
                if (lbtnsua != null)
                {
                    if (objchucnang.ISEDIT == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnsua.Visible = false;
                    }
                }
              
                if (lbtnxoa != null)
                {
                    if (objchucnang.ISDELETE == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnxoa.Visible = false;
                    }
                }
            }
        }

        protected void Grthue_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            LoadHienthi();
            Grthue.PageIndex = e.NewSelectedIndex;
            Grthue.DataBind();
        }
    }
}